package com.spring.mybatis.anno.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.spring.mybatis.anno.po.User;
import com.spring.mybatis.anno.service.UserService;



@RestController
public class UserController {
  @Autowired
  UserService userService;

  @RequestMapping(value = "/users", method = RequestMethod.GET)
  public List<User> getAll() {
    return userService.selectAll();
  }

  @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
  public User getById(@PathVariable("id") String id) {
    return userService.selectById(id);
  }

  @RequestMapping(value = "/user", method = RequestMethod.POST)
  public User add(@RequestBody User user) {
    Long insert = userService.insert(user);
    System.out.println("添加的id=[" + insert + "]的用户");
    return user;
  }

  @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
  public User update(@PathVariable Long id, @RequestBody User user) {
    user.setId(id);
    userService.update(user);
    return user;
  }

  @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
  public boolean delete(@PathVariable String id) {
    return userService.delete(id);
  }

  @PostMapping(value = "transactionTest")
  public Boolean transactionTest() {
    userService.saveTransactionDb();
    return true;
  }
}
